/**
 * Created by WUWE4 on 8/3/2017.
 */
$(document).ready(function () {
    $.ajax({
        url: "MerchantListServlet",
        method: "GET",
        success: function (data) {
            console.log(data);
                data.merchants.forEach(function (item, index, arr) {
                    var merchants = $("#merchants");
                    var imageUrl = "ImageLoader?path=" + item.imageUrl;
                    var link = "dishes.html?mId=" + item.mId;
                    merchants.append(
                        "<div class='col-md-4'>" +
                        "<div class='thumbnail'>" +
                        "<img src='" + imageUrl + "'>" +
                        "<div class='caption'>" +
                        "<h3>" + item.mName + "</h3>" +
                        "<p>" + item.address + "</p>" +
                        "<a href='" + link + "'>Read More</a>" +
                        "</div>" +
                        "</div> " +
                        "</div>");
                });

                data.dishtype.forEach(function (item, index, arr) {
                    var dishtype = $("#dishtype");
                    var liId = "li" + item.pId;
                    dishtype.append(
                        "<li id='"+liId+"'><a href='#'>"+ item.pname + "</a></li>"
                    );
                    $("#"+liId).on("click", function (event) {
                        $.ajax({
                            url:"MerchantListServlet?type=" + item.pId,
                            method:"GET",
                            success:function (data) {
                                //根据菜品种类找商家
                            	var merchants = $("#merchants");
                            	merchants.html("");
                            	data.merchants.forEach(function (item, index, arr) {
                                    var imageUrl = "ImageLoader?path=" + item.imageUrl;
                                    var link = "dishes.html?mId=" + item.mId;
                                    merchants.append(
                                        "<div class='col-md-4'>" +
                                        "<div class='thumbnail'>" +
                                        "<img src='" + imageUrl + "'>" +
                                        "<div class='caption'>" +
                                        "<h3>" + item.mName + "</h3>" +
                                        "<p>" + item.address + "</p>" +
                                        "<a href='" + link + "'>Read More</a>" +
                                        "</div>" +
                                        "</div> " +
                                        "</div>");
                                });
                            }
                        });
                    });
                });
                
                if(data.loginToken) {
                	var customer = data.loginToken;
                	$("#header").append(
                		"<label style='font-size: x-large'>"+ 
                		customer.cname + 
                   		"</label>&nbsp"
                	);
                	$("#header").append(
                    	"<a style='font-size: x-large' href='LogoutServlet'>"+ 
                    	"Logout" + 
                       	"</a>"
                    );
                } else {
                	$("#header").append(
                    	"<a style='font-size: x-large' data-toggle='modal' data-target='#LoginForm'>"+ 
                    	"Login" + 
                       	"</a>&nbsp"
                    );
                	$("#header").append(
                        "<a style='font-size: x-large' data-toggle='modal' data-target='#RegistForm'>"+ 
                        "Regist" + 
                        "</a>"
                    );
                }
            }
    });
    // var data = {
    //     "merchants": [{
    //         "mId": "001",
    //         "mName": "west",
    //         "address": "zhu",
    //         "imageUrl": "#",
    //         "idCardUrl": "#"
    //     }, {"mId": "002", "mName": "Louis", "address": "ITA", "imageUrl": "#", "idCardUrl": "#"}, {
    //         "mId": "003",
    //         "mName": "Barry",
    //         "address": "China",
    //         "imageUrl": "#",
    //         "idCardUrl": "#"
    //     }, {"mId": "004", "mName": "pauline", "address": "tang", "imageUrl": "#", "idCardUrl": "#"}],
    //     "dishtype":[{"pId":"001","pName":"All"},{"pId":"002","pName":"粤菜"},{"pId":"003","pName":"湘菜"},{"pId":"004","pName":"川菜"},{"pId":"005","pName":"甜食"}]
    // };
    // var list = JSON.parse(data);

    $("#loginbtn").on("click", function (event) {
        var username = $("#username1").val();
        var password = $("#password1").val();
        var logindata = {
            username: username,
            password: password
        };
        $.ajax({
            url:"LoginServlet",
            method: "POST",
            data: logindata,
            success: function (data) {
                //登录成功
            	console.log(data);
            	window.location.href=window.location.href;
            }
        });
    });

    $("#regbtn").on("click", function (event) {
        var username = $("#username2").val();
        var password = $("#password2").val();
        var regdata = {
            username: username,
            password: password
        };
        $.ajax({
            url:"RegisterServlet",
            method: "POST",
            data: regdata,
            success: function (data) {
            	console.log(data);
                //注册成功
            	if(data.status == "200") {
            		alert("注册成功");
            	} else {
            		alert("注册失败");
            	}
            	window.location.href=window.location.href;
            }
        });
    });
});